package com.example.homework.activity;



import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.homework.R;
import com.example.homework.bean.SnakeGameView;

public class SnakeGameActivity extends AppCompatActivity {
    private SnakeGameView gameView;
    private TextView scoreTextView;
    private Button startButton;
    private Button pauseButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_snake_game);

        gameView = findViewById(R.id.gameView);
        scoreTextView = findViewById(R.id.scoreTextView);
        startButton = findViewById(R.id.startButton);
        pauseButton = findViewById(R.id.pauseButton);

        gameView.setScoreListener(new SnakeGameView.ScoreListener() {
            @Override
            public void onScoreUpdated(int score) {
                if (score == -1) {
                    scoreTextView.setText("Game Over! Final Score: " + scoreTextView.getText().toString().split(": ")[1]);
                } else {
                    scoreTextView.setText("Score: " + score);
                }
            }

            @Override
            public void onGameOver() {
                // 游戏结束时启用 Start 按钮，禁用 Pause 按钮
                startButton.setEnabled(true);
                pauseButton.setEnabled(false);
            }
        });

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameView.startGame();
                startButton.setEnabled(false);
                pauseButton.setEnabled(true);
            }
        });

        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameView.pauseGame();
                startButton.setEnabled(true);
                pauseButton.setEnabled(false);
            }
        });

        // 设置方向按钮
        findViewById(R.id.upButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameView.setDirection(SnakeGameView.Direction.UP);
            }
        });

        findViewById(R.id.downButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameView.setDirection(SnakeGameView.Direction.DOWN);
            }
        });

        findViewById(R.id.leftButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameView.setDirection(SnakeGameView.Direction.LEFT);
            }
        });

        findViewById(R.id.rightButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameView.setDirection(SnakeGameView.Direction.RIGHT);
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (gameView != null) {
            if (!gameView.isGameOver() && gameView.isPaused()) {
                gameView.resumeGame();
                startButton.setEnabled(false);
                pauseButton.setEnabled(true);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (gameView != null) {
            gameView.pauseGame();
            startButton.setEnabled(true);
            pauseButton.setEnabled(false);
        }
    }
}